package com.eboss2.sbs.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.enums.EnumConsts;
import com.eboss2.sbs.http.API;
import com.eboss2.sbs.http.HttpCallback;
import com.eboss2.sbs.model.OrderDetailEntity;
import com.eboss2.sbs.model.db.OrderDetail;
import com.google.gson.reflect.TypeToken;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.utils.TimeUtil;
import com.mytools.tool.views.MyPictureDialog;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TradeDetailActivity extends BaseActivity implements View.OnClickListener {
	
	private String tradeOrderNo;
	private OrderDetail orderDetail;
	private OrderDetailEntity myData;
	private ImageView img_state;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_trade_detail);
		MyActivityManager.getInstance().addActivity(this);
		initView();
	}
	
	private void initView(){
		RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
		relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "交易详情 ");
		
		img_state = (ImageView) findViewById(R.id.img_state);
		
		findViewById(R.id.btn_point_url).setOnClickListener(this);
		findViewById(R.id.btn_coupon_url).setOnClickListener(this);
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		
		tradeOrderNo = getIntent().getStringExtra("tradeOrderNo");
		
		orderDetail = (OrderDetail) getIntent().getSerializableExtra("orderDetail");
		
		if(StringUtil.isNull(tradeOrderNo)){
			//本地数据
			if(orderDetail!=null){
				myData = new OrderDetailEntity(orderDetail);
				setTvText(R.id.next_sure, "上报");
				showView(R.id.next_sure, true);
				findViewById(R.id.next_sure).setOnClickListener(this);
				display();
			}
			else{
				toast("传参有误");
				finish();
			}
		}
		//网络数据
		else{
			getdata();
		}
		
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
//		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("tradeOrderNo", tradeOrderNo);
		
		API.http_post(this, API.ORDER_DETAIL, params, new HttpCallback<OrderDetailEntity>(this) {
			@Override
			public void onSuccess(int statusCode, OrderDetailEntity data) {
				if(data==null)return;
				myData = data;
				display();
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<OrderDetailEntity>() {
				};
			}
		});
	}
	
	private void display(){
		EnumConsts.TradeState tradeState = EnumConsts.TradeState.getByState(myData.getState());
		setTvText(R.id.tv_state, tradeState.getName());
		img_state.setImageResource(tradeState.getImg());
		setTvText(R.id.tv_trade_money, StringUtil.getMoneyString(myData.getTradeMoney())+"元");
		if(StringUtil.isNull(myData.getMemberName())){showView(R.id.ly_member_name, false);}
		else{
			showView(R.id.ly_member_name, true);
			setTvText(R.id.tv_member_name, myData.getMemberName());
		}
		setTvText(R.id.tv_pay_type, EnumConsts.SqbPayChannel.getByType(myData.getPayType()).getName());
		setTvText(R.id.tv_terminal_sn, myData.getTerminalSn());
		setTvText(R.id.tv_terminal_name, myData.getTerminalName());
		setTvText(R.id.tv_trade_order_no, myData.getTradeOrderNo());
		setTvText(R.id.tv_trade_money_sum, StringUtil.getMoneyString(myData.getTradeMoney())+"元");
		setTvText(R.id.tv_trade_money_real, StringUtil.getMoneyString(myData.getRealMoney())+"元");
		
		showView(R.id.layout_benifit, !StringUtil.isNull(myData.getBenefit()));
		
		setTvText(R.id.tv_benefit, myData.getBenefit());
		setTvText(R.id.tv_point, myData.getPointCost()+"点");
		setTvText(R.id.tv_trade_time, TimeUtil.getStringFromDate(new Date(myData.getTradeTime()), "yyyy年MM月dd日 HH:mm:ss"));
		
		if(StringUtil.isNull(tradeOrderNo)){showView(R.id.layout_member_use_count, false);}
		else{
			showView(R.id.layout_member_use_count, true);
			setTvText(R.id.tv_count, myData.getCount()+"次");
		}
		
		String pointUrl = myData.getPoint_url();
		String couponUrl = myData.getCoupon_url();
		int point = myData.getPoint();
		
		//当不存在积分活动和优惠券活动时，如果有积分则会利用积分二维码字段返回交易流水二维码 这里需要处理
//		showView(R.id.layout_point_url, StringUtil.isNull(pointUrl)?false:true);
//		if(!StringUtil.isNull(pointUrl)&&point==0){
//			setTvText(R.id.btn_point_url, "扫描查看交易详情");
//		}
//
//		showView(R.id.layout_coupon_url, StringUtil.isNull(couponUrl)?false:true);
//		showView(R.id.layout_youhui_info, (StringUtil.isNull(couponUrl)&&StringUtil.isNull(pointUrl))?false:true);
//		showView(R.id.layout_point_cost, StringUtil.isNull(tradeOrderNo)?false:true);
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.next_sure:
				if (orderDetail.getPayType() == EnumConsts.SqbPayChannel.WALLET.getType()){
					return;
				}
				doSubmit();
				break;
			case R.id.btn_coupon_url:
				String pointUrl = myData.getPoint_url();
				int point = myData.getPoint();
				if(!StringUtil.isNull(pointUrl)&&point==0){showPicture("扫描查看交易详情", myData.getCoupon_url());}
				else{
					showPicture("使用微信扫一扫 获取更多优惠信息", myData.getCoupon_url());
				}
				break;
			case R.id.btn_point_url:
				showPicture("使用微信扫一扫 获取更多优惠信息", myData.getPoint_url());
				break;
			default:break;
		}
	}
	
	private void showPicture(String title, String url){
		MyPictureDialog.Builder builder = new MyPictureDialog.Builder(this);
		builder.setTitle(title);
		builder.setPicture(url);
		MyPictureDialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
	}
	
	private void doSubmit(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", orderDetail.getSid());
		String cardNo = orderDetail.getCardNo();
		String password = orderDetail.getPassword();
		String couponSns = orderDetail.getCouponSns();
		if(!StringUtil.isNull(cardNo)){params.put("cardNo", cardNo);}
		else{params.put("cardNo", "");}
		if(!StringUtil.isNull(password)){params.put("password", password);}
		else{params.put("password", "");}
		if(!StringUtil.isNull(couponSns)){params.put("couponSns", couponSns);}
		else{params.put("couponSns", "");}
		
		params.put("cash", orderDetail.getCash());
		params.put("bankAmount", orderDetail.getBankAmount());
		
		params.put("couponCoverAmount", orderDetail.getCouponCoverAmount());
		params.put("pointCoverAmount", orderDetail.getPointCoverAmount());
		params.put("clientOrderNo", orderDetail.getTradeOrderNo());
		
		String activityCode = orderDetail.getActivateCode();
		if(StringUtil.isNull(activityCode)){params.put("activateCode", "");}
		else{params.put("activateCode", activityCode);}
		
		params.put("merchantNo", orderDetail.getMerchantNo());
		params.put("t", orderDetail.getTradeTime());
		params.put("pointAmount", orderDetail.getPointAmount());
		
		String transNo = orderDetail.getTransNo();
		String authCode = orderDetail.getAuthCode();
		if(!StringUtil.isNull(transNo)){params.put("transNo", transNo);}
		else{params.put("transNo", "");}
		if(!StringUtil.isNull(authCode)){params.put("authCode", authCode);}
		else{params.put("authCode", "");}
		
		params.put("serialNum", orderDetail.getSerialNum());
		params.put("payType", orderDetail.getPayType());
		
		API.http_post(this, API.TRADE_LOG_UPLOAD, params, new HttpCallback<Object>(this) {
			@Override
			public void onSuccess(int statusCode, Object obj) {
				toast("上报成功!");
			}

			@Override
			public void onFailure(int statusCode, String message) {
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<Object>() {
				};
			}
		});
	}

}
